package com.fw.service.property.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.custom.PropertyPlaceholderConfigurer;

import com.fw.service.bean.EntryBean;
import com.fw.service.property.PropertyService;

public class PropertyServiceImpl implements PropertyService {

    private static Logger log = Logger.getLogger(PropertyServiceImpl.class.getName());

    @Override
    public String getProperty(String key) {
	log.debug("getProperty key = " + key);
	return PropertyPlaceholderConfigurer.getProperty(key);
    }

    @Override
    public String getPropertyWithDefaultValue(String key, String defaultValue) {
	log.debug("getProperty with defaultValue");
	String value = PropertyPlaceholderConfigurer.getProperty(key);
	return value != null ? value : defaultValue;
    }

    @Override
    public List<EntryBean<String, String>> getPropertyAll() {
	log.debug("load");
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	Set<Entry<String, String>> entrieSet = PropertyPlaceholderConfigurer.getAllProperties().entrySet();
	if (entrieSet != null) {
	    for (Entry<String, String> entry : entrieSet) {
		entryBeans.add(new EntryBean<String, String>(entry.getKey(), entry.getValue()));
	    }
	}
	return entryBeans;
    }

    @Override
    public List<EntryBean<String, String>> selectPropertyByPrefixKey(String prefixKey) {
	log.debug("selectPropertyByPrefix");
	List<EntryBean<String, String>> entryBeans = new ArrayList<EntryBean<String, String>>();
	Set<Entry<String, String>> entrieSet = PropertyPlaceholderConfigurer.getAllProperties().entrySet();
	if (entrieSet != null) {
	    for (Entry<String, String> entry : entrieSet) {
		if (entry.getKey().startsWith(prefixKey)) {
		    entryBeans.add(new EntryBean<String, String>(entry.getKey(), entry.getValue()));
		}
	    }
	}
	return entryBeans;
    }

}
